var member;
$(function(){
	var maId=GetQueryString("maId");
	member=getEntity("memberInfo");
//	console.log(maId)
	$.ajax({
		type:'post',
		url:rootPath+'/address/selectAddressById',
		data:{'maId':maId},
		success:function(data){
			$("#mId").val(member.mId);
			if(data.resultDesc){
				var address=data.data;
			//	console.log(address);
				if(address){
					$("#name").val(address.name);
					$("#maId").val(address.maId);
					$("#tel").val(address.tel);
					$("#street").val(address.street);
					$("#isdefault").val(address.isdefault);
					$("#region").text(address.province+address.city+address.county);
					$("#province").val(address.province);
					$("#city").val(address.city);
					$("#county").val(address.county);
					$("#provinceCode").val(address.provinceCode);
					$("#cityCode").val(address.cityCode);
					$("#countyCode").val(address.countyCode);
					$("#mId").val(address.mId);
					if(address.isdefault=='1'){
						$('.selectI').attr('src',rootPath+'/image/agree.png');
					}else{
						$('.selectI').attr('src',rootPath+'/image/agree1.png');
					}
				}
			}
			
		}
	});
	/*$('#tel').change(function(){
		var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(17[0]{9})|(18[0-9]{1}))+\d{8})$/; 
		if (!myreg.test($(this).val())) { 
			var message = "手机号码格式不正确!!!";
			return false;
		}
		if(isNaN($(this).val())){
			alert("联系电话不能是汉字")
			return false;
		}
		if($(this).val().length>11){
			alert("联系电话不能超过11位")
			return false;
		}
	});*/
	var isdefault = rootPath+'/image/agree.png';
	/**提交数据******/
	$("#confirmUpdateAddress").click(function(){
		var selectI = $(".moAdre .selectI").attr("src");
		if(selectI!=null && isdefault == selectI){
			$("#isdefault").val("1");
		}else{
			$("#isdefault").val("0");
		}
		if(!$("#name").val()){
			alert("请输入收货人姓名");
			return false;
		}
		var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(17[0]{9})|(18[0-9]{1}))+\d{8})$/; 
		var tel = $("#tel").val();
		if(!tel){
			alert("请输入联系电话");
			return false;
		}
		if (!myreg.test(tel)) { 
			alert("手机号码格式不正确!!!");
			return false;
		}
		if(isNaN(tel)){
			alert("联系电话不能是汉字")
			return false;
		}
		if(tel.length>11){
			alert("联系电话不能超过11位")
			return false;
		}
		
		if(!$("#region").text()){
			alert("请选择所在地区");
			return false;
		}
		if(!$("#street").val()){
			alert("请输入详细地址");
			return false;
		}
		
		var data = $("form").serialize();
		$.ajax({
			type:"post",
			url:rootPath + "/address/addOrUpdateAddress",
			dataType:"JSON",
			data:data,
			success:function(data){
				if(data.resultSuccess){
					var orderAdressT = $("#orderAdressT").val();
					if(orderAdressT!=null && orderAdressT!='' && orderAdressT=="1"){
						window.location.href=rootPath+"/address/maAdress";
					}else{
						window.location.href=rootPath+"/member/vip_set_address";
					}
					
				}
			}
		});
	})
	
})